package com.baijiayun.live.ui.onlineuser

import androidx.lifecycle.MutableLiveData
import com.baijiayun.live.ui.base.BaseViewModel
import com.baijiayun.livecore.context.LiveRoom
import com.baijiayun.livecore.models.LPGroupItem
import com.baijiayun.livecore.models.LPUserModel
import com.baijiayun.livecore.models.imodels.IUserModel
import com.baijiayun.livecore.utils.LPLogger
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit

/**
 * Created by yongjiaming on 2019-10-26
 * Describe:
 */
class OnlineUserViewModel(val liveRoom: LiveRoom) : BaseViewModel() {
    val onlineUserCount = MutableLiveData<Int>()
    val onlineUserList = MutableLiveData<List<IUserModel>>()
    val onlineUserGroup = MutableLiveData<List<LPGroupItem>>()

    val userList = ArrayList<IUserModel>()

    override fun subscribe() {
        liveRoom.onlineUserVM
            .observableOfOnLineUserCount
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : DisposingObserver<Int>() {
                override fun onNext(t: Int) {
                    onlineUserCount.value = t
                }
            })

        liveRoom.onlineUserVM
            .observableOfOnlineUser
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : DisposingObserver<List<IUserModel>>() {
                override fun onNext(t: List<IUserModel>) {
                    userList.clear()
                    if (liveRoom.onlineUserVM.enableGroupUserPublic()) {
                        if (!liveRoom.onlineUserVM.groupList.isNullOrEmpty()) {
                            userList.addAll(liveRoom.onlineUserVM.groupList[0].userModelList)
                        }
                    } else {
                        userList.addAll(t)
                    }
                    onlineUserList.value = t
                    onlineUserCount.value = liveRoom.onlineUserVM.allCount
                }
            })

        liveRoom.onlineUserVM
            .observableOfOnGroupItem
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : DisposingObserver<List<LPGroupItem>>() {
                override fun onNext(t: List<LPGroupItem>) {
                    //只看本组，不更新组信息
                    if (liveRoom.onlineUserVM.enableGroupUserPublic() && !liveRoom.onlineUserVM.enableMyGroupUsersPublish()) {
                        onlineUserGroup.value = t
                    }
                    onlineUserList.value = userList
                    onlineUserCount.value = liveRoom.onlineUserVM.allCount
                }
            })
        liveRoom.onlineUserVM.requestGroupInfoReq()
        onlineUserCount.value = liveRoom.onlineUserVM.allCount
        loadMore(liveRoom.currentUser.group)
    }

    fun loadMore(groupId: Int = -1) {
        liveRoom.onlineUserVM.loadMoreUser(groupId)
    }

    fun getUser(position: Int): IUserModel? = if (liveRoom.onlineUserVM.enableGroupUserPublic() && liveRoom.onlineUserVM.enableMyGroupUsersPublish()
        && liveRoom.currentUser.group != 0
    ) {
        //分组且只看本组的非大班成员
        val lpGroupItem = liveRoom.onlineUserVM.groupMap[liveRoom.currentUser.group]
        if (position < liveRoom.onlineUserVM.userCount) {
            //大班课老师助教
            liveRoom.onlineUserVM.getUser(position)
        } else {
            //本组成员
            val size = lpGroupItem?.userModelList?.size ?: 0
            if (size <= position - liveRoom.onlineUserVM.userCount) {
                null
            } else {
                lpGroupItem?.userModelList?.get(position - liveRoom.onlineUserVM.userCount)
            }
        }
    } else {
        if (position >= 0 && position < userList.size) {
            userList[position]
        } else {
            null
        }
    }

    fun getCount(): Int = if (liveRoom.onlineUserVM.enableGroupUserPublic() && liveRoom.onlineUserVM.enableMyGroupUsersPublish()
        && liveRoom.currentUser.group != 0
    ) {
        val lpGroupItem = liveRoom.onlineUserVM.groupMap[liveRoom.currentUser.group]
        (lpGroupItem?.userModelList?.size ?: 0) + liveRoom.onlineUserVM.userCount
    } else {
        userList.size
    }


    fun getAssistantLabel(): String = liveRoom.customizeAssistantLabel

    fun getGroupId() = liveRoom.currentUser.group

    fun updateGroupInfo(item: LPGroupItem) {
        liveRoom.onlineUserVM.loadMoreUser(item.id)
    }
}